package com.saiyaemon.blog.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.abel533.entity.Example;
import com.github.abel533.entity.Example.Criteria;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.saiyaemon.blog.mapper.ArticleMapper;
import com.saiyaemon.blog.mapper.ArticleTagMapper;
import com.saiyaemon.blog.mapper.CategoryMapper;
import com.saiyaemon.blog.mapper.TagMapper;
import com.saiyaemon.blog.pojo.Article;
import com.saiyaemon.blog.pojo.ArticleTag;
import com.saiyaemon.blog.pojo.Category;
import com.saiyaemon.blog.pojo.MyMap;
import com.saiyaemon.blog.pojo.Tag;
import com.saiyaemon.blog.service.ArticleService;
import com.saiyaemon.blog.utils.TimeUtil;

/**
 * @author 作者 SaiyaEmon
 * @date 创建时间：2017年11月30日 下午8:21:16
 */
@Service("articleService")
@Transactional
public class ArticleServiceImpl implements ArticleService {

	@Autowired
	ArticleMapper articleMapper;

	@Autowired
	CategoryMapper categoryMapper;

	@Autowired
	ArticleTagMapper articleTagMapper;

	@Autowired
	TagMapper tagMapper;

	@Override
	public List<Article> selectByExample(Article article) {
		Example example = new Example(Article.class);
		Criteria criteria = example.createCriteria();
		criteria.andLike("title", "%" + article.getTitle() + "%");
		List<Article> list = articleMapper.selectByExample(example);
		return list;
	}

	@Override
	public int insert(Article article) {
		return articleMapper.insertSelective(article);
	}

	@Override
	public List<Object> selectAllInformation(List<Article> articles) {
		List<Object> articleInformations = new ArrayList<>();

		for (Article article : articles) {			
			Map<String, Object> articleInformation = selectInformation(article);
			// 将一篇博客所有信息 保存到 博客信息列表中
			articleInformations.add(articleInformation);
		}
		return articleInformations;
	}

	@Override
	public Map<String, Object> selectInformation(Article article) {
		Map<String, Object> articleInformation = new HashMap<>();
		List<Tag> tags = new ArrayList<>();
		// 查找分类名称
		Category category = new Category();
		category.setId(article.getCategoryId());
		Category selectCategory = categoryMapper.selectOne(category);

		// 查找博客包含的标签
		ArticleTag articleTag = new ArticleTag();
		articleTag.setArticleId(article.getId());
		List<ArticleTag> articleTags = articleTagMapper.select(articleTag);

		for (ArticleTag articleTag2 : articleTags) {
			Tag tag = new Tag();
			tag.setId(articleTag2.getTagId());
			tag = tagMapper.selectOne(tag);
			tags.add(tag);
		}
		articleInformation.put("category", selectCategory);
		articleInformation.put("article", article);
		articleInformation.put("tags", tags);
		articleInformation.put("time", TimeUtil.formatTime(article.getCreateTime()));
		articleInformation.put("updateTime", TimeUtil.formatTime(article.getUpdateTime()));
		return articleInformation;
	}

	@Override
	public Article selectById(Integer id) {
		return articleMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Article> select(Article article) {
		return articleMapper.select(article);
	}

	@Override
	public int selectCount(Article article) {
		return articleMapper.selectCount(article);
	}

	@Override
	public int update(Article article) {
		return articleMapper.updateByPrimaryKeySelective(article);
	}

}
